home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / daemons / nfs / nfs-serv.2be / nfs-serv / nfs-server-2.2beta16 / setattr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-11  |  3.0 KB  |  128 lines

  1. /*
  2.  * getattr    This module handles the NFS attributes.
  3.  *
  4.  * Authors:    Mark A. Shand, May 1988
  5.  *        Donald J. Becker, <becker@super.org>
  6.  *        Rick Sladkey, <jrs@world.std.com>
  7.  *        Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  8.  *        Olaf Kirch, <okir@monad.swb.de>
  9.  *
  10.  *        Copyright 1988 Mark A. Shand
  11.  *        This software maybe be used for any purpose provided
  12.  *        the above copyright notice is retained.  It is supplied
  13.  *        as is, with no warranty expressed or implied.
  14.  */
  15.  
  16. #include "nfsd.h"
  17.  
  18. /*
  19.  * Set file attributes based on file handle
  20.  */
  21. nfsstat fh_setattr(fh, attr, s, rqstp, flags)
  22. nfs_fh        *fh;
  23. sattr        *attr;
  24. struct stat    *s;
  25. struct svc_req    *rqstp;
  26. int        flags;
  27. {
  28.     char *path;
  29.     nfsstat status;
  30.  
  31.     if ((path = fh_path(fh, &status)) == NULL) {
  32.         dprintf(D_CALL, "setattr failed: No such file.\n");
  33.         return (NFSERR_STALE);
  34.     }
  35.     return setattr(path, attr, s, rqstp, flags);
  36. }
  37.  
  38. /*
  39.  * Set file attributes given the path. The flags argument
  40.  * determines if we have to stat the file or if the stat buf
  41.  * passed in s contains valid data.
  42.  * As we go along and modify the file attributes, we update the
  43.  * fields of this stat structure.
  44.  */
  45. nfsstat setattr(path, attr, s, rqstp, flags)
  46. char        *path;
  47. sattr        *attr;
  48. struct stat    *s;
  49. struct svc_req    *rqstp;
  50. int        flags;
  51. {
  52.     struct stat    sbuf;
  53.  
  54.     if (s == NULL) {
  55.         s = &sbuf;
  56.         flags |= SATTR_STAT;
  57.     }
  58.  
  59.     if ((flags & SATTR_STAT) && lstat(path, (s = &sbuf)) < 0) {
  60.         dprintf(D_CALL, "setattr: couldn't stat %s! errno=%d\n",
  61.                 path, errno);
  62.         return nfs_errno();
  63.     }
  64.  
  65.     if (flags & SATTR_SIZE) {
  66.         unsigned int    size = attr->size;
  67.  
  68.         if (S_ISREG(s->st_mode) && size != -1) {
  69.             if (truncate(path, size) < 0)
  70.                 goto failure;
  71.             s->st_size = size;
  72.         }
  73.     }
  74.  
  75.     if (flags & SATTR_UTIMES) {
  76.         unsigned int    a_secs = attr->atime.seconds;
  77.         unsigned int    m_secs = attr->mtime.seconds;
  78.  
  79.         if ((a_secs != -1 && a_secs != s->st_atime)
  80.          || (m_secs != -1 && m_secs != s->st_mtime)) {
  81.             struct timeval tvp[2];
  82.             tvp[0].tv_sec = attr->atime.seconds;
  83.             tvp[0].tv_usec = attr->atime.useconds;
  84.             tvp[1].tv_sec = attr->mtime.seconds;
  85.             tvp[1].tv_usec = attr->mtime.useconds;
  86.             if (utimes(path, tvp) < 0)
  87.                 goto failure;
  88.             if (attr->mtime.seconds != -1)
  89.                 s->st_atime = attr->atime.seconds;
  90.             if (attr->mtime.seconds != -1)
  91.                 s->st_mtime = attr->mtime.seconds;
  92.         }
  93.     }
  94.  
  95.     if (flags & SATTR_CHMOD) {
  96.         unsigned int    mode = attr->mode;
  97.  
  98.         if (mode != -1 && (mode & 07777) != (s->st_mode & 07777)) {
  99.             if (chmod(path, mode) < 0)
  100.                 goto failure;
  101.             s->st_mode = (s->st_mode & ~07777) | (mode & 07777);
  102.         }
  103.     }
  104.  
  105.     if (flags & SATTR_CHOWN) {
  106.         uid_t        uid = attr->uid;
  107.         gid_t        gid = attr->gid;
  108.  
  109.         if (uid != (uid_t)-1)
  110.             uid = luid(uid, nfsmount, rqstp);
  111.         if (gid != (gid_t)-1)
  112.             gid = lgid(gid, nfsmount, rqstp);
  113.  
  114.         if ((uid != (uid_t)-1 && uid != s->st_uid)
  115.          || (gid != (gid_t)-1 && gid != s->st_gid)) {
  116.             if (lchown(path, uid, gid) < 0)
  117.                 goto failure;
  118.             if (uid != (uid_t)-1) s->st_uid = uid;
  119.             if (gid != (gid_t)-1) s->st_gid = gid;
  120.         }
  121.     }
  122.  
  123.     return (NFS_OK);
  124.  
  125. failure:
  126.     return nfs_errno();
  127. }
  128.